package cdu.sl.aichatdemo.controller;

import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgent;
import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgentOptions;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @Description
 * @Author sunlin5@asiainfo.com
 * @Date 2025/8/27
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/bailian")
public class BailianAgentRagController {

    private  DashScopeAgent agent;

    public BailianAgentRagController(DashScopeAgentApi dashscopeAgentApi) {
        this.agent = new DashScopeAgent(dashscopeAgentApi);
    }

    @Value("${spring.ai.dashscope.agent.app-id}")
    private String appId;

    @GetMapping(value = "/chat", produces = "text/event-stream;charset=utf-8")
    public Flux<String> stream(@RequestParam(value = "message") String message) {
        return agent.stream(new Prompt(message, DashScopeAgentOptions.builder().withAppId(appId).build())).map(response -> {
            AssistantMessage app_output = response.getResult().getOutput();
            log.info("app_output: {}", app_output);
            return app_output.getText();
        });
    }


}
